package com.atguigu.gmall.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.product.client.ProductClientFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.context.IContext;

import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: lx
 * @Time: 15:55
 * @Description: 首页
 */
@Controller
public class IndexController {

    @Autowired
    private ProductClientFeign productClientFeign;

    //进入首页  https://www.jd.com/
/*    @GetMapping("/")
    public String index() {
        //转发页面
        return "index";
    }*/
    @GetMapping("/")
    public String index(Model model) {
        //获取数据
        List<Map<String, Object>> listMap = getData();
        model.addAttribute("list", listMap);
        return "index/index";
    }

    @Autowired
    private TemplateEngine templateEngine;//执行静态化的类

    //人为触发 静态化程序进行生成静态化页面  首页
    @GetMapping("/createHtml")
    @ResponseBody
    public Result createHtml() {
        //数据
        Context iContext = new Context();
        List<Map<String, Object>> listMap = getData();
        iContext.setVariable("list", listMap);
        //流 写入流  不要乱
        Writer writer = null;
        try {
            //writer = new FileWriter("D:\\temp\\index.html");
//            writer = new OutputStreamWriter(new
//                    FileOutputStream("D:\\temp\\index.html"),
//                    "UTF-8");
            writer = new PrintWriter(new File("D:\\temp\\index.html"),"UTF-8");
            //生成首页
            //参数1： 页面"index/index"
            //参数2：数据
            //参数3：流  写操作
            templateEngine.process("index/index"
                    , iContext, writer);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != writer)
                    writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return Result.ok();
    }

    private  List<Map<String, Object>> getData() {
        //现在有的数据 一二三级分类关联数据  视图
        List<BaseCategoryView> categoryViewList = productClientFeign.getCategoryViewList();
        //业务处理   自定义POJO  无敌版的POJO   Map
        List<Map<String, Object>> listMap = new ArrayList<>();
        //根据一级分类ID进行分组
        //Map0 K:Long categry1Id 1  : V:List<BaseCategoryView> 长度 60
        //Map1 K:Long categry1Id 2  : V:List<BaseCategoryView> 长度 25
        //Map2
        //Map16
        Map<Long, List<BaseCategoryView>> listMap1Group = categoryViewList.stream().collect(
                Collectors.groupingBy(BaseCategoryView::getCategory1Id));
        int index = 0;
        for (Map.Entry<Long, List<BaseCategoryView>> entry1 : listMap1Group.entrySet()) {
            Map<String, Object> resultMap1 = new HashMap<>();
            List<BaseCategoryView> entry1Value = entry1.getValue();
            //index
            resultMap1.put("index", ++index);
            resultMap1.put("categoryId", entry1.getKey());
            resultMap1.put("categoryName", entry1Value.get(0).getCategory1Name());
            //: K   13
            Map<Long, List<BaseCategoryView>> listMap2Group = entry1Value.
                    stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            List<Map<String, Object>> listMap2 = new ArrayList<>();
            for (Map.Entry<Long, List<BaseCategoryView>> entry2 : listMap2Group.entrySet()) {
                Map<String, Object> resultMap2 = new HashMap<>();
                List<BaseCategoryView> entry2Value = entry2.getValue();
                resultMap2.put("categoryId", entry2.getKey());
                resultMap2.put("categoryName", entry2Value.get(0).getCategory2Name());

                List<Map<String, Object>> listMap3 = new ArrayList<>();
                for (BaseCategoryView entry3 : entry2Value) {
                    Map<String, Object> resultMap3 = new HashMap<>();
                    resultMap3.put("categoryId", entry3.getCategory3Id());
                    resultMap3.put("categoryName", entry3.getCategory3Name());
                    listMap3.add(resultMap3);
                }
                resultMap2.put("categoryChild", listMap3);
                listMap2.add(resultMap2);
            }
            resultMap1.put("categoryChild", listMap2);
            listMap.add(resultMap1);
        }

        return listMap;
    }


}
